package com.dataaccess.dataaccess.support.fluoriteCloud;

import cn.hutool.json.JSONObject;
import com.dataaccess.dataaccess.common.base.BaseService;
import com.dataaccess.dataaccess.web.service.DeviceAboatFirewaterService;
import com.dataaccess.dataaccess.web.service.VisualizationVideoAlarmService;
import com.ys.product.ysmq.front.log.Log;
import com.ys.product.ysmq.front.log.LogFactory;
import com.ys.product.ysmq.front.msg.AbstractConsumerMessage.ConsumerCallBack;
import com.ys.product.ysmq.front.msg.StandardConsumerMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 读提交模式的示例demo
 * </p>
 * 
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2018年5月31日
 * @modify by reason:{方法名}:{原因}
 */
@Service
@Slf4j
public class ReadCommitMessagePatternMain  extends BaseService {
    // 26810f3acd794862b608b6cfbc32a6b8
    private static String appKey = "65a6b87b5e994b9dad18316fb9030153";

    // 3155063e93f09f377eaf5ba9f321f8c2
    private static String appSecret = "c9d3ec9adf2b314f000f78bb45a1a668";

    // private static Logger logger = LoggerFactory.getLogger(ReadCommitMessagePatternMain.class);

    private static String group = "group1";

    static int printListMaxSize = 100;

    private static Log log = LogFactory.getLog(DeviceAboatFirewaterService.class);

    @Resource
    VisualizationVideoAlarmService visualizationVideoAlarmService;

//    public static void main(String[] args) throws IOException {
////        init();
////    }

    public void init(){
        // 开放平台的url,这是test环境的url https://test.ys7.com:65
        String path = "https://open.ys7.com";
        //
        // 设置你的appkey,appSecret,group
        StandardConsumerMessage consumerMessage = new StandardConsumerMessage(path, appKey, appSecret, group);
        // 设置调用接口的间隔时间,ms
        consumerMessage.setConsumeIntervalTime(1000);
        // 设置消费消息的回调接口,建议把消息放入本地队列,不要在回调函数里做业务处理.
        consumerMessage.setConsumerCallBack(new ConsumerCallBack() {
            @Override
            public void consumerCall(List<Object> msg) {
                if (msg.size() > 100) {
                    // 消息太多,只打印前100个
                    log.info("msgSize:{},msg:{}", msg.size(), msg.subList(0, 100));
                } else {
                    log.info("msgSize:{},msg:{}", msg.size(), msg);
                }
                for (Object object : msg) {
                    // class com.alibaba.fastjson.JSONObject
                    log.info("my readMessageHandle object:{},class:{}", object, object.getClass());
                    com.alibaba.fastjson.JSONObject aliJson = (com.alibaba.fastjson.JSONObject)object;
                    JSONObject jsonObject = new JSONObject();
                    Set<String> set = aliJson.keySet();
                    for(String key : set){
                        jsonObject.put(key,aliJson.get(key));
                    }
                    try {
                        visualizationVideoAlarmService.fluoriteCloud(jsonObject);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        consumerMessage.initClient();
    }
}
